package org.example.service.impl;

import org.example.Mapper.ActivityMapper;
import org.example.Mapper.ActivityRecordMapper;
import org.example.Mapper.UserMapper;
import org.example.domain.Activity;
import org.example.domain.ActivityRecord;
import org.example.domain.User;
import org.example.service.ActivityRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

@Service
public class ActivityRecordServiceImpl implements ActivityRecordService {

    @Autowired
    private ActivityRecordMapper activityRecordMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer updateActivityRecordIsDoByActivityRecordId(Integer activityRecordId) {//
        ActivityRecord activityRecord=activityRecordMapper.getActivityRecordById(activityRecordId);
        if (activityRecord.getIsDo()==2){//缺席改正为签到
            activityRecordMapper.changeIsDoToOneById(activityRecordId);//先改为签到
            if(activityRecord.getIsPunish()==0){//未做出处罚时，不作任何处理
                ;
            } else if(activityRecord.getIsPunish()==1){//之前就此记录作出过处罚，接触其处罚并将该记录设置为未处罚
                activityRecordMapper.changeIsPunishToZeroByActivityRecordId(activityRecordId);//将该条记录设置为未处罚
                userMapper.changePunishDateById(activityRecord.getUserId(),new Date());//更新其封禁日期，进行解禁
            }
            return 0;
        } else if (activityRecord.getIsDo()==1){
            if(activityRecord.getStatus()==1){//该记录正在申请中
                return 1;
            } else if(activityRecord.getStatus()==2){//该记录已经申请通过了
                return 2;
            } else if(activityRecord.getStatus()==0){//该记录还未申请
                activityRecordMapper.changeIsDoToTwoById(activityRecordId);//先改为缺席
                ActivityRecord[] activityRecords1=activityRecordMapper
                        .getAllActivityRecordsByIsDoAndIsPunishAndUserId(activityRecord.getUserId());//获取这条活动记录的
                // 报名者目前有几条还未被处罚的记录
                if(activityRecords1.length==3){//未处罚的记录到达了三条，对其进行处罚
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Calendar calendar=Calendar.getInstance();//获取日历
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE,90);
                    userMapper.changePunishDateById(activityRecord.getUserId(),
                            calendar.getTime());//对该学生做出三个月不能报名任何活动的处罚
                    activityRecordMapper.changeIsPunishToOneByUserId(activityRecord.getUserId());//处罚后将缺席的记录标记未
                    // 已经处罚了
                }
                return 0;
            }
        }
        return 0;
    }

    @Override
    public ActivityRecord[] getActivityRecordsByActivityId(Integer activityId) {
        return activityRecordMapper.getActivityRecordsByActivityId(activityId);
    }

    @Override
    public ActivityRecord[] getActivityRecordsByUserId(Integer userId) {
        ActivityRecord[] activityRecords=activityRecordMapper.getActivityRecordsByUserId(userId);
        for (ActivityRecord activityRecord : activityRecords) {
            Activity activity=activityMapper.getAllById(activityRecord.getActivityId());
            activityRecord.setName(activity.getName());
            activityRecord.setDate(activity.getDate());
            activityRecord.setAddress(activity.getAddress());
            activityRecord.setTag(activity.getTag());
            switch (activity.getTag()){
                case "0":
                    activityRecord.setTag("思政培根");
                    break;
                case "1":
                    activityRecord.setTag("学科启智");
                    break;
                case "2":
                    activityRecord.setTag("体育强魄");
                    break;
                case "3":
                    activityRecord.setTag("美育润心");
                    break;
                case "4":
                    activityRecord.setTag("劳动强基");
                    break;
                case "5":
                    activityRecord.setTag("创新创业");
                    break;
                case "6":
                    activityRecord.setTag("其他");
                    break;
                default:
                    activityRecord.setTag("未分类");
            }
        }
        return activityRecords;
    }

    @Override
    public Integer addActivityRecord(ActivityRecord activityRecord) {//用户报名活动
        User user=userMapper.getAllById(activityRecord.getUserId());//获取报名者信息
        if(user.getPunishDate().getTime()-new Date().getTime()>0){//用户还处于处罚时间，不能进行报名
            return 3;
        }
        Integer[] userIds=activityRecordMapper.getUserIdsByActivityId(activityRecord.getActivityId());//先获取所有报名参加了
        // 这项活动的人员id，要避免重复报名
        boolean isIn=false;
        for (Integer userId : userIds) {
            if (Objects.equals(userId, activityRecord.getUserId())) {
                isIn = true;
                break;
            }
        }
        if(isIn){//该学生已报名参加了这项活动，禁止其再次报名
            return 0;
        } else {//该同学未参加这项活动，查看是否还有报名余额
            Activity activity=activityMapper.getAllById(activityRecord.getActivityId());//通过活动id获取该项活动的全部信息
            if(activity.getStillHave()==0){//报名余额为0，阻止用户报名
                return 1;
            } else if(activity.getIsSignIn()==1) {//活动已经签到过了，无法再报名
                return 4;
            } else {//还有报名余额，进行报名
                activityRecordMapper.addActivityRecord(activityRecord.getUserId(),activityRecord.getActivityId());
                activityMapper.reduceStillHaveById(activityRecord.getActivityId());//减少一个报名余额
                return 2;
            }
        }
    }

    @Override
    public ArrayList<User> getUsersByActivityId(Integer activityId) {
        Integer[] userIds= activityRecordMapper.getUserIdsByActivityId(activityId);//先获取参加此项活动所有人员的id
        ArrayList<User> users=new ArrayList<>();
        for (Integer userId : userIds) {
            User user=userMapper.getAllById(userId);//根据id查出所有信息
            users.add(user);//将结果添加返回
        }
        return users;
    }

    @Override
    public void deleteActivityRecordById(Integer id,Integer activityId) {//用户取消活动报名，同时释放报名余额
        activityRecordMapper.deleteActivityRecordById(id);//删除活动记录
        activityMapper.addStillHaveById(activityId);//增加该活动的报名余额
    }

    @Override
    public Integer deleteActivityRecordByUserIdAndActivityId(Integer userId, Integer activityId) {
        ActivityRecord activityRecord=activityRecordMapper.getActivityRecordByUserIdAndActivityId(userId,activityId);
        Activity activity=activityMapper.getAllById(activityId);//查询活动
        if (activityRecord==null){
            return 0;//未查询到该条报名记录
        } else if(activity.getIsSignIn()==1){//判断活动是否已经签到
            return 2;
        } else {
            activityRecordMapper.deleteActivityRecordByUserIdAndActivityId(userId,activityId);//删除该条记录
            activityMapper.addStillHaveById(activityId);//增加该活动的报名余额
            return 1;
        }
    }
}